//
//  ContactsController.swift
//  HHSwift
//
//  Created by 华宏 on 2020/9/8.
//  Copyright © 2020 huahong. All rights reserved.
//

import UIKit
import ContactsUI
class ContactsController: BaseViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        self.title = "通讯录"
        
        let contactPicker = CNContactPickerViewController()
        contactPicker.delegate = self
        //要显示在联系人详细信息卡中的CNContact属性,如果未设置，则显示所有属性
        contactPicker.displayedPropertyKeys = [CNContactPhoneNumbersKey]
        self.present(contactPicker, animated: true, completion: nil)
        
        
    }
    

    

}

//MARK: - CNContactPickerDelegate
extension ContactsController:CNContactPickerDelegate {
  

      //选中单个联系人
     func contactPicker(_ picker: CNContactPickerViewController, didSelect contact: CNContact){
        
        for labelValue:CNLabeledValue in contact.phoneNumbers {
            let phoneNumber:CNPhoneNumber = labelValue.value
            let phone:String = phoneNumber.stringValue
            
            print("phone:",phone)
            print("familyName:",contact.familyName)

            
        }
     }

    //选中单个属性
    func contactPicker(_ picker: CNContactPickerViewController, didSelect contactProperty: CNContactProperty){
        
        print("familyName:",contactProperty.contact.familyName)

        if contactProperty.key == CNContactPhoneNumbersKey {
            
            let phoneNumber:CNPhoneNumber = contactProperty.value as! CNPhoneNumber
            let phone:String = phoneNumber.stringValue
            print("phone:",phone)
            
        } else if contactProperty.key == CNContactPostalAddressesKey {
            
            let address:CNPostalAddress = contactProperty.value as! CNPostalAddress
            let street = address.street
            print("street:",street)
            
        }else{
            print("value:",contactProperty.value as Any)

        }
    }

      
//     //选中多个联系人
//    func contactPicker(_ picker: CNContactPickerViewController, didSelect contacts: [CNContact]){
//
//    }
//
//    //选中多个属性
//    func contactPicker(_ picker: CNContactPickerViewController, didSelectContactProperties contactProperties: [CNContactProperty]){
//
//    }
    
    //取消
    func contactPickerDidCancel(_ picker: CNContactPickerViewController){
       
    }
}
